<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%">
    <div v-loading="formLoading" class="device-key-list-container">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
        <el-form-item label="密钥" prop="secretKey">
          <el-input v-model="queryParams.secretKey" placeholder="请输入密钥" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.YK_KEY_STATE)" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="queryParams.remark" placeholder="请输入备注" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" /> 搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" /> 重置
          </el-button>
        </el-form-item>
      </el-form>

      <div class="table-container">
        <!-- 列表 -->
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column label="ID" align="center" prop="id" width="100px" />
          <el-table-column label="密钥" align="center" prop="secretKey" />
          <el-table-column label="状态" align="center" prop="status" width="100px">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.YK_KEY_STATE" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="所属设备" align="center" prop="deviceName">
            <template #default="scope">
              <span v-if="scope.row.deviceName">{{ scope.row.deviceName }}</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column label="所属用户" align="center" prop="userName" width="100px">
            <template #default="scope">
              <span v-if="scope.row.userName">{{ scope.row.userName }}</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
          <el-table-column label="操作" align="center" width="120px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="copyToClipboard(scope.row.secretKey)"
              >
                <Icon icon="ep:document-copy" class="mr-5px" /> 复制秘钥
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
          @pagination="getList" />
      </div>
    </div>
  </Dialog>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { DeviceKeyApi, DeviceKey } from '@/api/yunkong/devicekey'

defineOptions({ name: 'DeviceKeyList' })

const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('查看秘钥') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中

const loading = ref(false) // 列表的加载中
const list = ref<DeviceKey[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  secretKey: undefined,
  status: undefined,
  remark: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  resetQuery()
  await getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceKeyApi.getDeviceKeyPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  queryParams.pageNo = 1
  queryParams.pageSize = 10
}

/** 复制到剪贴板 */
const copyToClipboard = (text: string) => {
  if (!text) return
  navigator.clipboard.writeText(text).then(() => {
    message.success('复制成功')
  }).catch(() => {
    message.error('复制失败，请手动复制')
  })
}
</script>

<style scoped>
.device-key-list-container {
  display: flex;
  flex-direction: column;
  height: 60vh;
}

.table-container {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 10px;
}

.pagination-container {
  padding: 10px 0;
}
</style>